package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.Coin;
import com.chen.dto.CoinDto;
import com.chen.dto.R;
import com.chen.fegin.CoinFegin;
import com.chen.service.CoinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Date: 2021/5/11 10:10 AM
 * @Version 1.0
 */
@RestController
@Api(tags = "币种控制器")
@RequestMapping("/coins")
public class CoinController implements CoinFegin {

    @Autowired
    private CoinService coinService;

    @GetMapping
    @ApiOperation(value = "分页条件查询数字货币")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,value = "数字货币的名称") ,
            @ApiImplicitParam(name = "type" ,value = "数字货币类型的名称") ,
            @ApiImplicitParam(name = "status" ,value = "数字货币类型的状态") ,
            @ApiImplicitParam(name = "title" ,value = "数字货币类型的标题") ,
            @ApiImplicitParam(name = "wallet_type" ,value = "数字货币钱包类型") ,
            @ApiImplicitParam(name = "current" ,value = "当前页") ,
            @ApiImplicitParam(name = "size" ,value = "每页显示的条数") ,
    })
    public R<Page<Coin>> list(  String name , String type , Byte status ,
                                String title ,@RequestParam(name = "wallet_type",required = false) String walletType ,
                                @ApiIgnore Page<Coin> page){

        QueryWrapper<Coin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.like(StringUtils.isNotBlank(type),"type",type);
        queryWrapper.like(status!= null,"status",status);
        queryWrapper.like(StringUtils.isNotBlank(walletType),"wallet",walletType);
        queryWrapper.orderByDesc("created");
        Page<Coin> list = coinService.page(page, queryWrapper);
        return R.ok(list);
    }

    @GetMapping("/all")
    public R<List<Coin>> all(Byte status){
        QueryWrapper<Coin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(status!= null,"status",status);
        queryWrapper.orderByDesc("created");
        List<Coin> list = coinService.list(queryWrapper);
        return R.ok(list);
    }

    @PostMapping
    public R add(@RequestBody Coin coin){
        coinService.save(coin);
        return R.ok();
    }

    @PatchMapping
    public R update(@RequestBody Coin coin){
        coinService.updateById(coin);
        return R.ok();
    }

    @PostMapping("/setStatus")
    public R setStatus(@RequestBody Coin coin){
        coinService.updateById(coin);
        return R.ok();
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        Coin coin = coinService.getById(id);
        return R.ok(coin);
    }


    @Override
    public Map<Long, CoinDto> getCoinByIds(List<Long> ids) {
        return coinService.getCoinByIds(ids);
    }
}
